import http from '@/utils/http-util'

let BASE_MENU_CONTENT = process.env.WEB_CONTEXT_PATH + 'sys/menu'
/** 获取用户菜单 **/
export function getUserMenuApi() {
  return http({
    url: BASE_MENU_CONTENT + '/user',
    method: 'get',
    params: http.queryParam(),
    loading: false
  })
}

/** 获取菜单树 **/
export function getMenuTreeApi() {
  return http({
    url: BASE_MENU_CONTENT + '/tree',
    method: 'get',
    params: http.queryParam(),
    loading: false
  })
}

/** 获取菜单信息 **/
export function getMenuInfoApi(menuId = 0) {
  return http({
    url: BASE_MENU_CONTENT + '/' + menuId,
    method: 'get',
    params: http.queryParam(),
    loading: false
  })
}

/** 菜单排序 **/
export function getMenuOrderApi(menuId = 0, index) {
  return http({
    url: BASE_MENU_CONTENT + '/' + menuId + '/order',
    method: 'patch',
    params: http.queryParam({ index }),
    loading: false
  })
}

/** 保存菜单 **/
export function saveMenuApi(data = {}) {
  return http({
    url: BASE_MENU_CONTENT,
    method: 'post',
    data: http.bodyData({
      parentId: data.parentId || 0,
      name: data.name,
      icon: data.icon,
      permission: data.permission,
      keepAlive: data.keepAlive,
      label: data.label,
      system: data.system,
      type: data.type || 0,
      urlType: data.urlType || 0,
      url: data.url
    }),
    loading: false
  })
}

/** 修改菜单 **/
export function updateMenuApi(data = {}) {
  if (!data.id || data.id === 0 || data.id === '0') return
  return http({
    url: BASE_MENU_CONTENT + '/' + data.id,
    method: 'put',
    data: http.bodyData({
      parentId: data.parentId || 0,
      name: data.name,
      icon: data.icon,
      permission: data.permission,
      keepAlive: data.keepAlive,
      label: data.label,
      system: data.system,
      fresh: data.fresh,
      type: data.type || 0,
      urlType: data.urlType || 0,
      url: data.url
    }),
    loading: false
  })
}

/** 单个删除菜单或权限 **/
export function deleteMenuOrPermissionApi(id = 0) {
  if (!id || id === 0 || id === '0') return
  return http({
    url: BASE_MENU_CONTENT + '/' + id,
    method: 'delete',
    params: http.queryParam(),
    loading: false
  })
}

/** 菜单下权限列表 **/
export function getMenuPermissionsApi(menuId, data = {}) {
  return http({
    url: BASE_MENU_CONTENT + '/' + menuId + '/permissions',
    method: 'post',
    data: http.bodyData({
      'page': data.page || 1,
      'pageSize': data.pageSize || 20,
      'sort': {
        'field': data.sortField,
        'rule': data.sortRule === undefined ? 1 : data.sortRule
      }
    })
  })
}

/** 批量删除权限 **/
export function batchDeleteMenuPermissionsApi(data = []) {
  return http({
    url: BASE_MENU_CONTENT + '/permissions',
    method: 'delete',
    data: http.bodyData({
      ids: data
    })
  })
}
